package CallableTest;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * @Author ufo
 * @Description TODO
 * @Date 2021/1/20 16:02
 * @Version 1.0
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Mythread mythread = new Mythread();
        //Thread中不能直接放入Callable，需要FutureTask来装载适配
        FutureTask future = new FutureTask(mythread);


        //有缓存
        new Thread(future, "A").start();
        new Thread(future, "B").start();
        new Thread(future, "C").start();

        //get可能会阻塞，因为他要等待线程走完了才能获取到返回值。所以这里我们通常放到最后get或者使用异步通信！
        System.out.println(future.get());

    }


}


class Mythread implements Callable<String> {

    @Override
    public String call() throws Exception {
        System.out.println("====子线程====");
        return "helloufo";
    }
}
